package com.mentalhealthplatform.studentV3.service;


import com.mentalhealthplatform.appModels.Student;
import com.mentalhealthplatform.global.rbac.model.User;
import com.mentalhealthplatform.studentV3.CustomUserDetailsV3;
import com.mentalhealthplatform.studentV3.repository.BaseUserRepositoryV3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author Pear
 */
@Service("studentUserDetailsServiceImpl")
public class UserDetailsServiceImpl implements UserDetailsService {

    private final BaseUserRepositoryV3 userRepository;

    @Autowired
    public UserDetailsServiceImpl(BaseUserRepositoryV3 userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public CustomUserDetailsV3 loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("User not found with username: " + username));

        // 使用 CustomUserDetails 封装用户信息，包括角色和权限
        return new CustomUserDetailsV3((Student) user);
    }
}
